<template>
  <div class="logincenter">
    <div class="login-box">
      <div :class="owlclass">
        <div class="hand"></div>
        <div class="hand hand-r"></div>
        <div class="arms">
          <div class="arm"></div>
          <div class="arm arm-r"></div>
        </div>
      </div>
      <div class="input-box">
        <el-form ref="formRef" :rules="rules" :model="form" class="w-[320px]">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              autocomplete="off"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              show-password
              size="large"
              @focus="focusSelect()"
              @blur="onBlur()"
            />
          </el-form-item>
          <el-form-item>
            <button @click="onSubmit" v-loading="loading">登 录</button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--滑块验证-->
    <el-dialog
      title="请拖动滑块完成拼图"
      width="360px"
      v-model="isShowSliderVerify"
      :close-on-click-modal="false"
      @closed="refresh"
      append-to-body
      style="top: 95px"
    >
      <slider-verify
        ref="sliderVerify"
        @success="onSuccess"
        @fail="onFail"
        @again="onAgain"
      />
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, onBeforeUnmount } from "vue";
import { login } from "@/api/admin/user";
import { showMessage } from "@/composables/util";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { setToken } from "@/composables/auth";
import SliderVerify from "@/components/SliderVerify.vue";
import { User, Lock } from "@element-plus/icons-vue";

const owlclass = ref("owl");
//输入框监听取得焦点
function focusSelect() {
  owlclass.value = "owl password";
}
// 失去焦点
function onBlur() {
  owlclass.value = "owl";
}

const router = useRouter();
const store = useStore();

const form = reactive({
  username: "",
  password: "",
  // 随机字符串
  nonceStr: "",
  // 验证值
  captchaValue: "",
});

const rules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
};

const formRef = ref(null);
const loading = ref(false);

// 是否显示滑块验证
const isShowSliderVerify = ref(false);

/* 滑动验证成功*/
function onSuccess(captcha) {
  loading.value = true;
  form.nonceStr = captcha.nonceStr;
  form.captchaValue = captcha.value;
  login(form)
    .then((res) => {
      if (res.success == true) {
        // 提示成功
        showMessage("登录成功", "success");
        // notification('登录成功')

        let token = res.data.token;
        // 存储 token
        setToken(token);
        isShowSliderVerify.value = false;
        // 跳转到后台页面
        router.push("/admin");
      } else {
        let message = res.message;
        showMessage(message, "error");
        isShowSliderVerify.value = false;
      }
    })
    .finally(() => {
      loading.value = false;
    });
}
/* 滑动验证失败*/
function onFail(msg) {
  showMessage(msg || "验证失败，请控制拼图对齐缺口", "error");
  refresh();
}

/* 滑动验证异常*/
function onAgain() {
  showMessage("滑动操作异常，请重试", "error");
  refresh();
}
/* 刷新验证码*/
//注意这里一定要和前面标签中的 ref属性的值一模一样
const sliderVerify = ref(null);
function refresh() {
  sliderVerify.value.refresh();

}

const onSubmit = () => {
  // 登录表单验证
  formRef.value.validate((valid) => {
    if (!valid) {
      console.log("验证不通过");
      return false;
    } else {
      isShowSliderVerify.value = true;
    }
  });
};
// 用户通过了验证

function onKeyUp(e) {
  if (e.key == "Enter") {
    onSubmit();
  }
}

// 添加键盘监听
onMounted(() => {
  console.log("添加键盘监听");
  document.addEventListener("keyup", onKeyUp);
  // 禁止body页面滚动
  // document.body.style.overflow = "hidden";
  // document.body.addEventListener(
  //   "touchstart",
  //   function (e) {
  //     e.preventDefault();
  //   },
  //   { passive: false }
  // );
});

// 移除键盘监听
onBeforeUnmount(() => {
  document.removeEventListener("keyup", onKeyUp);
  // 恢复页面滚动
  // document.body.style.overflow = "scroll";
  // document.removeEventListener(
  //   "touchstart",
  //   function (e) {
  //     e.preventDefault();
  //   },
  //   { passive: false }
  // );
});
</script>

<style scoped>
.logincenter {
  /* 100%窗口高度 */
  height: 100vh;
  /* 弹性布局 居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  /* 渐变背景 */
  background: linear-gradient(
    125deg,
    #2c3e50,
    #2980b9,
    #27ae60,
    #e74c3c,
    #8e44ad
  );
  /* 指定背景图像的大小 */
  background-size: 500%;
  /* 执行动画：动画名 时长 线性的 无限次播放 */
  animation: bgAnimation 15s linear infinite;
}
/* 定义动画 */
@keyframes bgAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
.login-box {
  /* 相对定位 */
  position: relative;
  width: 320px;
}
.input-box {
  /* 弹性布局 垂直排列 */
  display: flex;
  flex-direction: column;
}
.input-box input {
  height: 40px;
  border-radius: 3px;
  /* 缩进15像素 */
  text-indent: 15px;
  outline: none;
  border: none;
  /* margin-bottom: 15px; */
}
.input-box button {
  /* 相对定位 */
  position: relative;
  width: 320px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 15px;
  color: #fff;
  /* 渐变背景 */
  background: linear-gradient(to right, #03a9f4, #f441a5, #ffeb3b, #09a8f4);
  /* 背景渐变色大小 */
  background-size: 400%;
  /* 圆角 */
  border-radius: 2px;
  z-index: 1;
  cursor: pointer;
}
/* 发光效果 */
.input-box button::before {
  content: "";
  position: absolute;
  top: -5px;
  left: -5px;
  bottom: -5px;
  right: -5px;
  /* 渐变背景 */
  background: linear-gradient(to right, #03a9f4, #f441a5, #ffeb3b, #09a8f4);
  /* 背景渐变色大小 */
  background-size: 400%;
  /* 圆角 */
  border-radius: 50px;
  /* 位于按钮之下 */
  z-index: -1;
  /* 设置模糊度 显示发光效果 */
  filter: blur(20px);
}
/* 鼠标移入执行动画 */
.input-box button:hover {
  /* 动画：名称 时间 infinite是无限次播放 */
  animation: streamer 8s infinite;
}
.input-box button:hover::before {
  animation: streamer 8s infinite;
}
/* 接下来定义动画 */
@keyframes streamer {
  100% {
    /* 背景位置 */
    background-position: -400% 0;
  }
}

/* 接下来是猫头鹰的样式 */
.owl {
  width: 211px;
  height: 108px;
  /* 背景图片 */
  background: url("@/assets/owl-login.png") no-repeat;
  /* 绝对定位 */
  position: absolute;
  top: -100px;
  /* 水平居中 */
  left: 50%;
  transform: translateX(-50%);
}
.owl .hand {
  width: 34px;
  height: 34px;
  border-radius: 40px;
  background-color: #472d20;
  /* 绝对定位 */
  position: absolute;
  left: 12px;
  bottom: -8px;
  /* 沿Y轴缩放0.6倍（压扁） */
  transform: scaleY(0.6);
  /* 动画过渡 */
  transition: 0.3s ease-out;
}
.owl .hand.hand-r {
  left: 170px;
}
.owl.password .hand {
  transform: translateX(42px) translateY(-15px) scale(0.7);
}
.owl.password .hand.hand-r {
  transform: translateX(-42px) translateY(-15px) scale(0.7);
}
.owl .arms {
  position: absolute;
  top: 58px;
  width: 100%;
  height: 41px;
  overflow: hidden;
}
.owl .arms .arm {
  width: 40px;
  height: 65px;
  position: absolute;
  left: 20px;
  top: 40px;
  background: url("@/assets/owl-login-arm.png") no-repeat;
  transform: rotate(-20deg);
  transition: 0.3s ease-out;
}
.owl .arms .arm.arm-r {
  transform: rotate(20deg) scaleX(-1);
  left: 158px;
}
.owl.password .arms .arm {
  transform: translateY(-40px) translateX(40px);
}
.owl.password .arms .arm.arm-r {
  transform: translateY(-40px) translateX(-40px) scaleX(-1);
}
</style>
